import { Component, OnDestroy, ViewChild, AfterViewInit } from '@angular/core';
import { Subscription } from 'rxjs/Subscription';
import { ManhuaPageProvider } from './manhua.page.provider';
import { ManhuaTable } from './table/manhua.table';
import { Router } from '@angular/router';
import { Util } from '../../util/util';


@Component({
  selector: 'manhua-page',
  templateUrl: './manhua.page.html',
  styleUrls: ['./manhua.page.css'],
  providers: [ManhuaPageProvider]
})

export class ManhuaPage {

  @ViewChild('manhuaTable') childComponent: ManhuaTable;

  searchTitle: string = '';

  finishs = [
    {
      text: '全部',
      value: 'all'
    }, {
      text: '连载中', value: false
    }, { text: '已完结', value: true }
  ];

  types = [
    { text: '全部', value: 'all' }, { text: '少年热血', value: '少年热血' }, { text: '武侠格斗', value: '武侠格斗' }, { text: '少女爱情', value: '少女爱情' }, { text: '运动竞技', value: '运动竞技' }, { text: '科幻魔幻', value: '科幻魔幻' }, { text: '幽默爆笑', value: '幽默爆笑' }, { text: '侦探推理', value: '侦探推理' }, { text: '东方同人', value: '东方同人' }, { text: '少年漫画', value: '少年漫画' }, { text: '少女漫画', value: '少女漫画' }, { text: '青年漫画', value: '青年漫画' }, { text: '女青漫画', value: '女青漫画' }, { text: '其他', value: '' }
  ];

  charets = ['all', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0-9', '其他'];

  updates = [{ text: '全部', value: 'all' }, { text: '最近', value: 'isUpdate' }];

  websites = [{ text: '全部', value: 'all' }, { text: '漫画人', value: '1kkk' }, { text: '动漫之家', value: 'dmzj' }, { text: '极速漫画', value: 'kuku' }];

  param: any = {
    type: 'all',
    isFinish: 'all',
    char: 'all',
    update: 'all',
    website: 'all'
  };

  constructor(private service: ManhuaPageProvider, private router: Router, private util: Util) {

    let _param = this.util.store().get('manhuaPageParam');

    if (_param) {
      this.param = _param;

      // console.log(_param);
    }

  }

  onKey(event) {
    console.log(event);

    if (event.keyCode == '13') {
      this.submit();
    }
  }

  // btn
  submit() {
    if (this.searchTitle == '') {
      return;
    }

    this.util.store().set('search', this.searchTitle);
    this.router.navigate(['/search']);
  }

  ngAfterViewInit() {


    setTimeout(() => {
      let data = Object.assign({}, this.param);
      this.childComponent.outputHandel(data);
    }, 100);


  }

  /**
   * 漫画分类
   * @param type 分类类型
   * @param value 值
   */
  search(type, value) {
    if (this.param[type] === value) {
      return;
    }
    this.param[type] = value;

    let data = Object.assign({}, this.param);

    this.childComponent.deleteData();
    this.childComponent.outputHandel(data);
  }


  ngOnDestroy() {
    this.util.store().set('manhuaPageParam', this.param);

    this.childComponent.storeData();
  }

}